package cn.zhangyong.dbframelearn

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import cn.zhangyong.dbframelearn.bean.Author
import cn.zhangyong.dbframelearn.bean.Note
import cn.zhangyong.dbframelearn.dao.AuthorDao
import cn.zhangyong.dbframelearn.dao.NoteDao
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

/**
 * ormlite学习module
 */
class MainActivity : AppCompatActivity() {
    val authors = ArrayList<Author>(30)
    val notes = ArrayList<Note>(200)

    init {
        var aRange = 0..20
        var nRange = 0..5
        for (i in aRange) {
            var author = Author()
            author.name = "刘备${i}"
            author.psw = "liubei${i}"
            authors.add(author)
            for (j in nRange) {
                var note = Note()
                note.author = author
                note.title = "${i}标题第${j}个"
                note.category = i * 10 + j
                note.content = "${i}都一样${j}"
                note.lastTime = Date().toString()
                notes.add(note)
            }
        }

    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var authorDao = AuthorDao(this)
        var noteDao = NoteDao(this)
        btn_add.setOnClickListener {
            for (author in authors) {
                authorDao.add(author)
            }
            for (note in notes) {
                noteDao.add(note)
            }
        }

        btn_show.setOnClickListener {
            var authorList = authorDao.queryForAll()
            var noteList = noteDao.queryForAll()
            tv.text = authorList.toString() + "\n" + noteList.toString()
        }
    }
}
